package com.situ.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.situ.bean.Staff;
import com.situ.bean.Work;

public interface WorkMapper {
	@Delete({ "delete from work", "where id = #{id,jdbcType=INTEGER}" })
	int deleteByPrimaryKey(Integer id);

	@Insert({ "insert into work ( staff_id, ", "project_id)", "values ( #{staffId,jdbcType=INTEGER}, ",
			"#{projectId,jdbcType=INTEGER})" })
	int insert(Work record);

	@Select({ "select", "id, staff_id, project_id", "from work", "where id = #{id,jdbcType=INTEGER}" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "staff_id", property = "staffId", jdbcType = JdbcType.INTEGER),
			@Result(column = "project_id", property = "projectId", jdbcType = JdbcType.INTEGER) })
	Work selectByPrimaryKey(Integer id);

	@Select({ "select", "id, staff_id, project_id", "from work" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "staff_id", property = "staffId", jdbcType = JdbcType.INTEGER),
			@Result(column = "project_id", property = "projectId", jdbcType = JdbcType.INTEGER) })
	List<Work> selectAll();

	@Update({ "update work", "set staff_id = #{staffId,jdbcType=INTEGER},",
			"project_id = #{projectId,jdbcType=INTEGER}", "where id = #{id,jdbcType=INTEGER}" })
	int updateByPrimaryKey(Work record);

	// 批处理
	@Insert({ "<script>", "insert into work (staff_id,project_id)", "values",
			"<foreach collection='staffs' item='staff' separator=','>", "(#{staff.id},#{id})", "</foreach>",
			"</script>" })
	int batchInsert(@Param("id") Integer id, @Param("staffs") List<Staff> staffs);

	// 根据Id删除关联
	@Delete("delete from work where project_id = #{projectId}")
	int deleteByProjectId(Integer projectId);

	// 根据项目id查询员工信息
	@Select({ "select staff.* from staff,work", "where staff.id=work.staff_id", "and work.project_id=#{projectId}" })
	List<Staff> selectByStaffProjectId(Integer projectId);
}